import { KeyGenerator } from '../../canvas/common/keygenerator.js';
import UtilFuncs from '../../canvas/common/tools/utilfuncs.js'
var utilfuncs = UtilFuncs.UtilFuncs;
import graphfactory from '../graphdatafactory.js'
var factory = graphfactory.GraphDataFactory;

var getfulltype = function (id) {
    var strID = id.toString(16);
    var strFulltype = strID.substr(0, 7);
    var type1 = parseInt(strFulltype, 16);
    var type = type1 & 0xffffffff;
    return type;
}

var FileDAO = {

}


FileDAO.parsingFileData = function(fileData)
{
    var dataList = new Array();
    var obj = JSON.parse(fileData);
    var version = obj["version"];
    var mapScale = obj["mapScale"];
    var fileKind = obj["fileKind"];
    var devlst = obj["devs"];
    this.parsingJsonArr_old(devlst,dataList);
    return dataList;
}

FileDAO.parsingFileData_new = function(fileData)
{
    var dataList = new Array();
    var obj = JSON.parse(fileData);
    var version = obj["version"];
    var mapScale = obj["mapScale"];
    var fileKind = obj["fileKind"];
    var devlst = obj["elements"];
    this.parsingJsonArr_new(devlst,dataList);
    return dataList;
}

FileDAO.parsingStreamData = function(obj)
{
    var dataList = new Array();
    //var obj = JSON.parse(streamData);
    var version = obj["version"];
    var mapScale = obj["mapScale"];
    var fileKind = obj["fileKind"];

    let scaling = obj["scaling"];
    let vleft = obj["vleft"];
    let vtop = obj["vtop"];
    let vright = obj["vright"];
    let vbottom = obj["vbottom"];
    let seed = obj["seed"];
    let backColor = obj["backColor"];
    if (!backColor){
        backColor = 0;
    }
    let projectScale = obj["projectScale"];
    if (!projectScale){
        projectScale = 1;
    }
    var devlst = obj["elements"];
    this.parsingJsonArr_new(devlst,dataList);
    return {dataList,scaling,vleft,vtop,vright,vbottom,seed,backColor,projectScale};
}

FileDAO.parsingJsonArr_old = function(devlst,dataList)
{
    for (var i = 0; i < devlst.length; i++) {
        var unitObj = devlst[i];
        var id = unitObj["id"];
        var strID = id.toString(16);
        var devFullType = getfulltype(id);
        var graphInfo = factory.create(devFullType);
        if (graphInfo != null) {
            graphInfo.fromJson(unitObj);
            dataList.push(graphInfo);
        }
    }
    return ;
}

FileDAO.parsingJsonArr_new = function(devlst,dataList)
{
    console.log("parsingJsonArr_new");
    let maxid = 0;
    for (var i = 0; i < devlst.length; i++) {
        var unitObj = devlst[i];
        maxid = Math.max(unitObj.gid);
    }
    KeyGenerator.init(maxid+1);
    for (var i = 0; i < devlst.length; i++) {
        var unitObj = devlst[i];

        dataList.forEach(ele => {
            if (ele.gid == unitObj.gid){
                unitObj.gid = KeyGenerator.gid();
            }
        });

        var type = unitObj["type"];
        var strID = type.toString(16);
        var graphInfo = factory.create(type);
        if (graphInfo != null) {
            graphInfo.fromJson(unitObj);
            dataList.push(graphInfo);
        }
    }
    return ;
}
export {FileDAO}



